package com.oocl.scan.jdt.parser;

import com.oocl.scan.utils.ScanConstants;
import java.util.List;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class ModifierParser {


  public static SingleMemberAnnotation getSingleMemberAnnotation(BodyDeclaration bodyDeclaration,String annotationName){
    List<Modifier> modifierList = bodyDeclaration.modifiers();
    if (modifierList==null){
      return null;
    }
    for (Object modifier:modifierList) {
      if (modifier instanceof SingleMemberAnnotation) {
        SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) modifier;
        String typeName = singleMemberAnnotation.getTypeName().getFullyQualifiedName();
        if (annotationName.equals(typeName)){
          return singleMemberAnnotation;
        }
      }
    }
    return null;
  }

  public static NormalAnnotation getAnnotation(BodyDeclaration bodyDeclaration,String annotationName){
    List modifierList = bodyDeclaration.modifiers();
    if (modifierList==null){
      return null;
    }
    for (Object modifier:modifierList) {
      if (modifier instanceof NormalAnnotation) {
        NormalAnnotation normalAnnotation = (NormalAnnotation) modifier;
        String typeName = normalAnnotation.getTypeName().getFullyQualifiedName();
        if (annotationName.equals(typeName)){
          return normalAnnotation;
        }
      }
    }
    return null;
  }

  public static String getAnnotationStringValueByName(NormalAnnotation normalAnnotation,String name){
    Expression expression = getAnnotationValueByName(normalAnnotation,name);
    if (expression!=null){
      return expression.toString().replace("\"","");
    }

    return "";
  }

  public static Expression getAnnotationValueByName(NormalAnnotation normalAnnotation,String name){
    Expression value = null;
    List values = normalAnnotation.values();
    for (Object valuePair: values) {
      if (valuePair instanceof MemberValuePair){
        MemberValuePair memberValuePair = (MemberValuePair) valuePair;
        if (name.equals(memberValuePair.getName().toString())){
          value = memberValuePair.getValue();
        }
      }

    }
    return value;
  }

  public static boolean isFinal(BodyDeclaration bodyDeclaration){
    List modifierList = bodyDeclaration.modifiers();
    return isHaveModifierByKeyword(modifierList, "final");
  }

  private static boolean isHaveModifierByKeyword(List<Modifier> modifierList, String aFinal) {
    for (Object obj : modifierList) {
      if (obj instanceof Modifier) {
        Modifier modifier = (Modifier) obj;
        if (modifier.getKeyword().toString().equals(aFinal)) {
          return true;
        }
      }

    }
    return false;
  }


  public static boolean isStatic(BodyDeclaration bodyDeclaration){
    List modifierList = bodyDeclaration.modifiers();
    return isHaveModifierByKeyword(modifierList, "static");
  }

  public static boolean isRestfulClass(TypeDeclaration typeDec) {
    SingleMemberAnnotation pathAnnotation = ModifierParser.getSingleMemberAnnotation(typeDec, ScanConstants.ANNOTATION_REST_PATH);
    return pathAnnotation != null ? true : false;
  }
}
